package com.pakazaka.testItemsSystem;

import com.pakazaka.enums.ItemPropertyType;
import com.pakazaka.layer.TiledLayer;
import com.pakazaka.player.BasicStranger;
import com.pakazaka.player.Player;
import com.pakazaka.simplecrawler.Core;
import static com.pakazaka.simplecrawler.Core.getInstance;
import java.awt.Graphics;
import java.awt.Image;
import java.util.Random;

/**
 * @author Riseremi
 */
public final class Item {

    private final Random rnd = new Random();
    protected char[] keys = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
    protected String id;
    protected Type type;
    protected char bind;
    protected int blocksX, blocksY;

    public Item(String id, Type type) {
        this.id = id;
        this.type = type;
        generateBind(Core.getInstance().getPlayer());
    }

    public Item(String id, Type type, int x, int y) {
        this.id = id;
        this.type = type;
        this.blocksX = x;
        this.blocksY = y;
    }

    public int getBlocksX() {
        return blocksX;
    }

    public void setBlocksX(int blocksX) {
        this.blocksX = blocksX;
    }

    public int getBlocksY() {
        return blocksY;
    }

    public void setBlocksY(int blocksY) {
        this.blocksY = blocksY;
    }

    public int getImageId() {
        return ItemPrototype.getInstance().getImageIdById(id);
    }

    public void paint(Graphics g, Image img) {
        TiledLayer layer = getInstance().getLayer();
        final int x = (layer.getBlocksX() + blocksX) * layer.getTileWidth();
        final int y = (layer.getBlocksY() + blocksY) * layer.getTileHeight();
        g.drawImage(img, x, y, null);
    }

    public void generateBind(BasicStranger hero) {
        char newBind = keys[rnd.nextInt(keys.length)];
        for (int i = 0; i < hero.getItems().size(); i++) {
            if (newBind == hero.getItems().get(i).getBind()) {
                newBind = keys[rnd.nextInt(keys.length)];
                i = 0;
            }
        }
        this.bind = newBind;
    }

    public boolean isPropertyExist(ItemPropertyType property) {
        String propertyString = property.name();
        String[] properties = ItemPrototype.getInstance().getPropertiesById(id);
        for (String string : properties) {
            if (string.equals(propertyString)) {
                return true;
            }
        }
        return false;
    }

    public char getBind() {
        return bind;
    }

    public String getName() {
        return ItemPrototype.getInstance().getNameById(id);
    }

    public String getId() {
        return id;
    }

    public String getDescription() {
        System.out.println("requested description of item " + id);
        return ItemPrototype.getInstance().getDescriptionById(id);
    }

    public Type getTypeInType() {
        return type;
    }

    public boolean isEffectExist(Player.EffectType effectToCheck) {
        Player.EffectType[] effects = ItemPrototype.getInstance().getEffectsById(id);
        for (Player.EffectType effect : effects) {
            if (effectToCheck == effect) {
                return true;
            }
        }
        return false;
    }

    public int getVolume() {
        return ItemPrototype.getInstance().getVolumeById(id);
    }

    public int getPower() {
        return ItemPrototype.getInstance().getPowerById(id);
    }

    /**
     *
     * @return sum of all protection types
     * @see Damage
     */
    public int getProtection() {
        return ItemPrototype.getInstance().getCutProtectionById(id)
                + ItemPrototype.getInstance().getBashProtectionById(id)
                + ItemPrototype.getInstance().getPierceProtectionById(id);
    }

    /**
     *
     * @param type damage type
     * @return single protection value
     */
    public int getProtection(Damage type) {
        switch (type) {
            case CUT:
                return ItemPrototype.getInstance().getCutProtectionById(id);
            case BASH:
                return ItemPrototype.getInstance().getBashProtectionById(id);
            case PIERCE:
                return ItemPrototype.getInstance().getPierceProtectionById(id);
            default:
                return 0;
        }
    }

    /**
     *
     * @return item id that appears after use of this item
     */
    public String getAfterUse() {
        return ItemPrototype.getInstance().getAfterUseById(id);
    }

    public Slot getWearSlot() {
        return ItemPrototype.getInstance().getWearSlot(id);
    }

    public enum Type {
        COLDARM, FIREARM, ARMOR, AMMO, CONTAINER, BOOK, TOOL, OTHER, MELEE, SUPPLY, TRASH
    }

    public enum Slot {
        BOOTS, HELMET, TSHIRT, MASK, GLASSES, LEGS, CLOAK, JACKET
    }

    /**
     * all types of damage and protection
     */
    public enum Damage {
        CUT, BASH, PIERCE
    }
}
